package com.demo;

import java.io.InputStream;
import java.util.zip.ZipInputStream;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.DeploymentBuilder;
import org.junit.Before;
import org.junit.Test;

public class ActivitiTest01{
	
	RepositoryService repositoryService = null;
	
	RuntimeService runtimeService = null;
	
	@Before
	public void setUp() throws Exception{
		// 创建流程引擎对象
		ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
		// 获取repositoryService
		repositoryService = processEngine.getRepositoryService();
	}
	
	/*
	 * 部署流程
	 */
	@Test
	public void deployProcess1(){
		// 创建部署对象
		DeploymentBuilder deploymentBuilder =repositoryService.createDeployment();
		// 加载流程的配置文件和图片
		deploymentBuilder.addClasspathResource("activiti_test01.bpmn")
							.addClasspathResource("activiti_test01.png");
		// 部署			 
		deploymentBuilder.deploy();
	}
	
	/*
	 * 部署流程   act_ge_bytearray, act_ge_property，act_re_deployment,act_re_procdef会有数据
	 */
	@Test
	public void deployProcessAddNameAndCategory(){
		// 创建部署对象
		DeploymentBuilder deploymentBuilder =repositoryService.createDeployment();
		// 加载流程的配置文件和图片
		deploymentBuilder.name("请假流程").category("请假类").addClasspathResource("activiti_test01.bpmn")
							.addClasspathResource("activiti_test01.png");
		// 部署			 
		deploymentBuilder.deploy();
	}
	
	/*
	 * 通过zip压缩文件部署流程,zip里包含.bpmn和.png,只是进行压缩
	 */
	@Test
	public void deployProcessByZip(){
		// 创建部署对象
		DeploymentBuilder deploymentBuilder =repositoryService.createDeployment();
		// 加载流程的配置文件和图片
		InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("activitiFile.zip");
		ZipInputStream zipInput = new ZipInputStream(inputStream);
		deploymentBuilder.addZipInputStream(zipInput)
			.name("请假流程")
			.category("请假类");
		
		// 部署			 
		deploymentBuilder.deploy();
	}
	
	/*
	 * 删除流程部署
	 */
	@Test
	public void deleteActivitiDeploy(){
		// act_re_deployment部署表的主键id
		String deploymentId = "801";
		// 如果流程已经启动，会报错
		repositoryService.deleteDeployment(deploymentId);
		
		String deploymentId1 = "701";
		//第二个参数是true级联删除，有数据，也能删除，false,有数据会报错
		repositoryService.deleteDeployment(deploymentId1, true);
	}
	
	
}
